<template>
	<view class="page">
		<custom-navigation title="调价详情">
		</custom-navigation>
		<view class="title-box">
			<view class="name">调价详情</view>
		</view>
		<view class="head-contaienr">
			<view class="item flex-row-center">
				<view class="value">调价单号：{{info.modify_price_no}}</view>
				<view :class="info.type=='已提交'?'bluebtn':'redbtn'">{{info.type}} </view>
			</view>
			<view class="item flex-row-center">
				<view class="value">创建时间：{{info.create_time}}</view>
			</view>

			<view class="item flex-row-center" v-if="info.type=='已提交'">
				<view class="value">提交时间：{{info.modify_time}}</view>
			</view>

			<view class="item flex-row-center" v-if="info.remark">
				<view class="value">调价备注：{{info.remark}}</view>
			</view>
		</view>

		<view class="title-box flex-row-center">
			<view class="name">商品明细</view>
			<view class="n1" style="margin-left: 80rpx;">共：{{datas.length}}种商品</view>
		</view>
		<scroll-view class="body-scroll" scroll-y="true">
			<view class="item-view" v-for="(item,index) in datas">

				<view class="flex-row-center" style="flex: 1;">
					<view class="name">{{item.goods_name}}</view>
					<view>新进货价：¥：<text style=" ">{{item.new_purchase_price}}</text></view>
				</view>

				<view class="flex-row-center" style="flex: 1;">
					<view style="flex: 1; margin: 4rpx 0;">{{item.goods_barcode}}</view>
					<view>新零售价：¥：<text style=" ">{{item.new_sale_price}}</text></view>
				</view>
				<view class="flex-row-center" style="margin-top: 4rpx;"  v-if="item.allow_member_price=='允许' && item.member_one_state=='启用'">
					<view style="flex: 1;margin: 4rpx 0;"> {{ }}</view>
					<view>{{item.member_one_name}}：¥：{{item.new_member_one}}</view>

				</view>
				<view class="flex-row-center" style="margin-top: 4rpx;"  v-if="item.allow_member_price=='允许' && item.member_two_state=='启用'">
					<view style="flex: 1;margin: 4rpx 0;"> {{ }}</view>
					<view>{{item.member_two_name}}：¥：{{item.new_member_two}}</view>

				</view>
				<view class="flex-row-center" style="margin-top: 4rpx;"  v-if="item.allow_member_price=='允许' && item.member_three_state=='启用'">
					<view style="flex: 1;margin: 4rpx 0;"> {{ }}</view>
					<view>{{item.member_three_name}}：¥：{{item.new_member_three}}</view>

				</view>

			</view>

		</scroll-view>

		<view class="bottom-container flex-row-center" v-if="!isEdit&&info.type!='已提交'">
			<view class="item" @click="settingMultiple(0)">删除</view>
			<view class="item" @click="settingMultiple(1)">编辑</view>
			<view class="item1" @click="settingMultiple(2)">提交</view>
		</view>
		<view v-else-if="isEdit" class="bottom-container flex-row-center">
			<view class="item" @click="settingMultiple(3)">保存草稿</view>
			<view class="item1" @click="settingMultiple(2)">提交</view>
		</view>

		<alert message="是否确定删除此调价单？" :show="delDialogShow" @leftAction="delDialogShow=false" @rightAction="del">
		</alert>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				delDialogShow: false,
				info: {},
				// original_sale_price：原销售价格(元)
				// new_sale_price：新销售价格(元)
				// member_one：原普通会员价(元)
				// now_member_one：新普通会员价(元)
				// member_two：原银卡会员价(元)
				// new_member_two：新银卡会员价(元)
				// member_three：原金卡会员价(元)
				// new_member_three：新金卡会员价(元)
				datas: [],
				countQty: 0,
				isEdit: false,
				goodsMemberLevelNameObj:{},
			}
		},
		onLoad(options) {
			this.info = JSON.parse(options.info)
			 
			this.getData()
			this.getInfo()
		},
		onShow() {
			if (uni.getStorageSync("refresh")) {
				this.getData()
				this.getInfo()
				uni.setStorageSync("refresh", null);
			}
		},
		methods: {
			getGoodsesMember(goods_no_s, callback) {
				if(!goods_no_s){
					if(callback){
						callback();
					}
					return;
				}

				let data = [];
				goods_no_s.forEach(item => {
					data.push({
						"colName": "goods_no",
						"ruleType": "eq",
						"required": true,
						"value": item
					})
				});


				this.$apiPost("/retail/select/srvretail_goods_member_select", {
					"serviceName": "srvretail_goods_member_select",
					"colNames": [
						"*"
					],

					relation_condition : {
						"data": [{
							"relation": "OR",
							"data": data
						}],
						"relation": "AND"
					}
				}).then(res => {
					let arr = res.data;
					if(arr){
						arr.forEach(item => {
							if(item && item.member_price_dis == '启用'){
								if(!this.goodsMemberLevelNameObj[item.goods_no]){
									this.goodsMemberLevelNameObj[item.goods_no]={}
								}
								this.goodsMemberLevelNameObj[item.goods_no][item.level] = item.member_level_name
							}
						});
					}
					if(callback){
						callback();
					}
				}).catch(function(){
					if(callback){
						callback();
					}
				})
			},
			settingMultiple(type) { //0 删除，1 编辑，2 提交
				if (type == 0) {
					this.delStock();
				} else if (type == 1) {
					// this.isEdit = true;
					let info = JSON.stringify(this.info)
					uni.navigateTo({
						url: '/pages/goods/modify_price/modify_add?info=' + info
					})
				} else if (type == 2) {
					this.save(1)
				} else if (type == 3) //修改
				{
					this.save(0)
				}
			},
			delStock() {
				this.delDialogShow = true;
			},
			del() {
				uni.showLoading({
					title: "删除中..."
				})
				this.$apiPost("/retail/operate/srvretail_modify_price_record_delete", [{
					"serviceName": "srvretail_modify_price_record_delete",
					"condition": [{
						"colName": "id",
						"ruleType": "in",
						"value": this.info.id
					}]
				}]).then(res => {
					uni.hideLoading()
					uni.setStorageSync("refresh", true)
					if (res.state == "SUCCESS") {
						uni.showToast({
							title: "成功！",
							mask: true
						})

						setTimeout(() => {
							uni.navigateBack()
						}, 1500)
					}
				}) 
			},
			save(type) { //type 0 保存草稿，1 提交。
				uni.showLoading({
					title: "加载中..."
				})
				
			
				this.$apiPost("/retail/operate/srvretail_modify_price_record_update", [{
					"serviceName": "srvretail_modify_price_record_update",
					"condition": [{
						"colName": "id",
						"ruleType": "eq",
						"value": this.info.id
					}],
					"data": [{
						"type": "已提交"
					}]
				}]).then(res => {
					uni.hideLoading()
					uni.setStorageSync("refresh", true)
					if (res.state == "SUCCESS") {
						uni.showToast({
							title: "成功！",
							mask: true
						})

						setTimeout(() => {
							uni.navigateBack()
						}, 1500)
					}
				})

			},
			getData() {
				uni.showLoading({
					title: "加载中..."
				})
				this.$apiPost("/retail/select/srvretail_modify_price_detail_select", {
					"serviceName": "srvretail_modify_price_detail_select",
					"colNames": ["*"],
					"condition": [{
						"colName": "modify_price_no",
						"ruleType": "eq",
						"value": this.info.modify_price_no
					}],
					"relation_condition": {},
					"page": {
						"pageNo": 1,
						"rownumber": 500
					}
				}).then(res => {	
					uni.hideLoading()
					if(res.data && res.data.length){
						let  arr = [];
						res.data.forEach(element => {
							arr.push(element.goods_no)
						});
						// this.getGoodsesMember(arr,()=>{
							this.datas = res.data;
						// })
					} else {
						this.datas = res.data;
					}
				 
				})
			},
			getInfo() {
			 
				this.$apiPost("/retail/select/srvretail_modify_price_record_select", {
					"serviceName": "srvretail_modify_price_record_select",
					"colNames": ["*"],
					"condition": [{
						"colName": "modify_price_no",
						"ruleType": "eq",
						"value": this.info.modify_price_no
					}],
					"relation_condition": {},
					"page": {
						"pageNo": 1,
						"rownumber": 500
					}
				}).then(res => {	
					this.info = res.data[0];
				  
				})
			}
		}
	}
</script>

<style scoped lang="scss">
	.page {
		background-color: white;
	}

	.title-box {
		width: 100%;
		height: 100rpx;
		background: rgba(211, 242, 234, 1);
		padding: 0 20rpx;

		.name {
			font-size: 32rpx;
			font-weight: 700;
			line-height: 100rpx;
			color: rgba(51, 51, 51, 1);
			flex: 1;
		}

		.n1 {
			font-size: 28rpx;
			font-weight: 500;
			line-height: 100rpx;
			color: rgba(51, 51, 51, 1);
		}
	}

	.head-contaienr {
		.item {
			padding: 10rpx 24rpx;

			.value {
				flex: 1;
				font-size: 28rpx;
				font-weight: 500;
				color: rgba(51, 51, 51, 1);
			}
		}

		.bluebtn {
			width: 124rpx;
			height: 48rpx;
			text-align: center;
			line-height: 48rpx;
			border-radius: 4rpx;
			border: 2rpx solid rgba(56, 192, 158, 1);
			margin-right: 22rpx;
			font-size: 28rpx;
			font-weight: 500;
			color: rgba(56, 192, 158, 1);
		}

		.redbtn {
			width: 124rpx;
			height: 48rpx;
			text-align: center;
			line-height: 48rpx;
			border-radius: 4rpx;
			border: 2rpx solid rgba(212, 48, 48, 1);
			margin-right: 22rpx;
			font-size: 28rpx;
			font-weight: 500;
			color: rgba(212, 48, 48, 1);
		}
	}

	.item-view {
		margin: 26rpx 20rpx 0 24rpx;
		padding-bottom: 18rpx;
		border-bottom: 2rpx solid rgba(245, 245, 247, 1);
		;

		.name {
			font-size: 28rpx;
			font-weight: 700;
			color: rgba(51, 51, 51, 1);
			flex: 1;
		}

		view {
			flex: 1;
			font-size: 28rpx;
			font-weight: 500;
			color: rgba(51, 51, 51, 1);
		}

	}

	.bottom-container {
		background-color: white;
		box-shadow: 0 -16rpx 16rpx 1rpx #00000008;
		height: 146rpx;
		padding: 0 24rpx;
		justify-content: space-around;

		.item {
			border-radius: 40rpx;
			line-height: 80rpx;
			text-align: center;
			border: 2rpx solid #333333FF;
			width: 192rpx;
			height: 80rpx;
			font-size: 30rpx;
			font-weight: 500;

		}

		.item1 {
			border-radius: 40rpx;
			line-height: 80rpx;
			text-align: center;
			border: 2rpx solid rgba(56, 192, 158, 1);
			background-color: rgba(56, 192, 158, 1);
			color: white;
			width: 192rpx;
			height: 80rpx;
			font-size: 30rpx;
			font-weight: 500;
		}

		.text {
			flex: 1;
			margin-left: 60rpx;
			font-size: 30rpx;
			font-weight: 700;

		}

		.btn {
			margin-right: 34rpx;
			width: 192rpx;
			height: 80rpx;
			border-radius: 40rpx;
			background: rgba(56, 192, 158, 1);
			font-size: 30rpx;
			font-weight: 700;
			color: white;
			text-align: center;
			line-height: 80rpx;
		}
	}
</style>
